<template>
  <el-dialog title="选择商品" :model-value="show" @close="$emit('hide')">
    <el-input
      placeholder="商品名称搜索"
      class="input-with-select"
      style="width: 30%; margin-bottom: 10px"
    >
      <template #append>
        <el-button :icon="Search" />
      </template>
    </el-input>
    <el-table
      style="width: 100%"
      border
      :data="list"
      cell-class-name="align-c"
      header-row-class-name="align-c"
      ref="multipleTableRef"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="商品名称" prop="name" />
      <el-table-column label="货号" width="100" prop="productSn">
        <template #default="{ row }">
          <span>NO.{{ row.productSn }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" prop="price"
        ><template #default="{ row }">
          <span>￥{{ row.price }}</span></template
        ></el-table-column
      >
    </el-table>
    <template #footer>
      <el-button>取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
    <div class="page">
      <el-config-provider :locale="locale">
        <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20, 50, 100]"
          background
          layout="sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="pageChangeHandle"
          @current-change="pageSizeChangeHandle"
      /></el-config-provider>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import useTable from "@/hooks/useTable";
import { Search } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { addProductRelationApi, getProductList } from "./api";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElMessage, ElMessageBox, ElTable } from "element-plus";
import { number } from "echarts";
import { useRoute } from "vue-router";
const locale = zhCn;
const multipleTableRef = ref<InstanceType<typeof ElTable>>();
const route = useRoute();
const form = reactive({
  keyword: "",
});
const multipleSelection = ref<IFlashProduct[]>([]);
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  id: {
    type: Number,
    default: 0,
  },
});
const emit = defineEmits(["hide"]);
const addList = ref<any>([]);
const handleSelectionChange = (val: IFlashProduct[]) => {
  console.log(val);

  addList.value = val.map((item: IFlashProduct) => ({
    productId: item.id,
    flashPromotionId: route.query.flashPromotionId as string,
    flashPromotionSessionId: route.query.flashPromotionSessionId as string,
  }));
  console.log(addList.value);
};

console.log(addList.value);

const submit = () => {
  ElMessageBox.confirm("确定添加商品吗？", "添加商品", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      addProductRelationApi(addList.value).then((res) => {
        if (res.code === 200) {
          console.log(res);
          ElMessage.success("添加成功");
          multipleTableRef.value!.clearSelection();
          emit("hide", true);
        }
      });
      console.log(addList.value);
    })
    .catch(() => {});
};

const {
  list,
  pageNum,
  total,
  pageChangeHandle,
  pageSizeChangeHandle,
  pageSize,
  searchParamsFn,
} = useTable(getProductList, form);
</script>

<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 14px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
